/*
 * IConnection.h
 *
 *  Created on: 09.08.2011
 *      Author: Blinov Kirill
 *   Copyright: LGPL
 *
 */

#ifndef ICONNECTION_H_
#define ICONNECTION_H_

#include <stddef.h>
#include "BasicTypes.h"
#include "RReadWriteImplementation.h"
#include "TThreadSafeQueue.h"
#include <vector>
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <boost/asio.hpp>

const UINT CONNECTION_OUTPUT_QUEUE_LENGHT = 15; //TODO подобрать подходящий размер
/*
 * IConnection - basic abstract class for both client and server connections wrappers
 */
class IConnection
{
private:
public:
	IConnection(){};
	virtual ~IConnection(){};

	virtual bool SendMessage( STANDART_NETWORK_MESSAGE* ) = 0;
	virtual void Start() = 0;
	virtual void Disconnect( const bool ) = 0;
};

class CConnectionLogic: public IConnection, public RReadWriteImplementation
{
private:
public:
	CConnectionLogic(){};
	CConnectionLogic( UINT ConnectionId,
					  boost::function<bool( const UINT, UINT64 )> ConnectionStatusCallback,
					  boost::asio::ip::tcp::endpoint *Endpoint,
					  boost::asio::io_service        *Io_service,
					  TThreadSafeQueue<STANDART_NETWORK_MESSAGE*>* IncomeQueue );
	virtual ~CConnectionLogic();

	virtual bool SendMessage( STANDART_NETWORK_MESSAGE* );
	virtual void Disconnect( const bool SendMessage );
};

class CServerConnection: public CConnectionLogic
{
private:
	boost::asio::ip::tcp::acceptor *acceptor;
public:
	CServerConnection(){};
	CServerConnection( UINT ConnectionId,
			           boost::function<bool( const UINT, UINT64 )> ConnectionStatusCallback,
			           boost::asio::ip::tcp::acceptor *Acceptor,
			           boost::asio::ip::tcp::endpoint *Endpoint,
			           boost::asio::io_service        *Io_service,
			           TThreadSafeQueue<STANDART_NETWORK_MESSAGE*>* IncomeQueue );
	virtual ~CServerConnection();

	virtual void Start();
};

class CClientConnection: public CConnectionLogic
{
private:

public:
	CClientConnection(){};
	CClientConnection(  UINT ConnectionId,
						boost::function<bool( const UINT, UINT64 )> ConnectionStatusCallback,
						boost::asio::ip::tcp::endpoint *Endpoint,
						boost::asio::io_service        *Io_service,
						TThreadSafeQueue<STANDART_NETWORK_MESSAGE*>* IncomeQueue );
	virtual ~CClientConnection();

	virtual void Start();
};

#endif /* ICONNECTION_H_ */
